﻿
Imports osi.root.delegates
Imports osi.root.formation
Imports osi.root.procedure
Imports osi.root.utils

Public Module _parameter_allocator
    Public Function parameter_allocator(Of T, PT1, PT2) _
                                       (ByVal i As Func(Of PT1, 
                                                           PT2, 
                                                           pointer(Of T), 
                                                           event_comb),
                                        ByRef o As allocator(Of Func(Of PT2, 
                                                                        pointer(Of T), 
                                                                        event_comb), 
                                                                PT1)) As Boolean
        If i Is Nothing Then
            Return False
        Else
            o = make_allocator(Function(v As PT1) As Func(Of PT2, pointer(Of T), event_comb)
                                   Return Function(p As PT2, r As pointer(Of T)) As event_comb
                                              Return i(v, p, r)
                                          End Function
                               End Function)
            Return True
        End If
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2) _
                                       (ByVal i As Func(Of PT1, 
                                                           PT2, 
                                                           pointer(Of T), 
                                                           event_comb)) _
                                       As allocator(Of Func(Of PT2, 
                                                               pointer(Of T), 
                                                               event_comb), 
                                                       PT1)
        Dim o As allocator(Of Func(Of PT2, pointer(Of T), event_comb), PT1) = Nothing
        assert(parameter_allocator(i, o))
        Return o
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2) _
                                       (ByVal i As _do_val_val_ref(Of PT1, 
                                                                      PT2, 
                                                                      T,
                                                                      Boolean),
                                        ByRef o As allocator(Of Func(Of PT2, 
                                                                        pointer(Of T), 
                                                                        event_comb), 
                                                                PT1)) As Boolean
        If i Is Nothing Then
            Return False
        Else
            o = make_allocator(Function(v As PT1) As Func(Of PT2, pointer(Of T), event_comb)
                                   Return Function(p As PT2, r As pointer(Of T)) As event_comb
                                              Return sync_async(Function() As Boolean
                                                                    Dim x As T = Nothing
                                                                    Return i(v, p, x) AndAlso
                                                                           eva(r, x)
                                                                End Function)
                                          End Function
                               End Function)
            Return True
        End If
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2) _
                                       (ByVal i As _do_val_val_ref(Of PT1, 
                                                                      PT2, 
                                                                      T,
                                                                      Boolean)) _
                                       As allocator(Of Func(Of PT2, 
                                                               pointer(Of T), 
                                                               event_comb), 
                                                       PT1)
        Dim o As allocator(Of Func(Of PT2, pointer(Of T), event_comb), PT1) = Nothing
        assert(parameter_allocator(i, o))
        Return o
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2) _
                                       (ByVal i As Func(Of PT1, 
                                                           PT2, 
                                                           T),
                                        ByRef o As allocator(Of Func(Of PT2, 
                                                                        pointer(Of T), 
                                                                        event_comb), 
                                                                PT1)) As Boolean
        If i Is Nothing Then
            Return False
        Else
            o = make_allocator(Function(v As PT1) As Func(Of PT2, pointer(Of T), event_comb)
                                   Return Function(p As PT2, r As pointer(Of T)) As event_comb
                                              Return sync_async(Function() As Boolean
                                                                    Return eva(r, i(v, p))
                                                                End Function)
                                          End Function
                               End Function)
            Return True
        End If
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2) _
                                       (ByVal i As Func(Of PT1, 
                                                           PT2, 
                                                           T)) _
                                       As allocator(Of Func(Of PT2, 
                                                               pointer(Of T), 
                                                               event_comb), 
                                                       PT1)
        Dim o As allocator(Of Func(Of PT2, pointer(Of T), event_comb), 
                              PT1) = Nothing
        assert(parameter_allocator(i, o))
        Return o
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3) _
                                       (ByVal i As Func(Of PT1, 
                                                           PT2, 
                                                           PT3, 
                                                           pointer(Of T), 
                                                           event_comb),
                                        ByRef o As allocator(Of Func(Of PT2, 
                                                                        PT3, 
                                                                        pointer(Of T), 
                                                                        event_comb), 
                                                                PT1)) As Boolean
        If i Is Nothing Then
            Return False
        Else
            o = make_allocator(Function(v As PT1) As Func(Of PT2, PT3, pointer(Of T), event_comb)
                                   Return Function(p As PT2, p2 As PT3, r As pointer(Of T)) As event_comb
                                              Return i(v, p, p2, r)
                                          End Function
                               End Function)
            Return True
        End If
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3) _
                                       (ByVal i As Func(Of PT1, 
                                                           PT2, 
                                                           PT3, 
                                                           pointer(Of T), 
                                                           event_comb)) _
                                       As allocator(Of Func(Of PT2, 
                                                               PT3, 
                                                               pointer(Of T), 
                                                               event_comb), 
                                                       PT1)
        Dim o As allocator(Of Func(Of PT2, PT3, pointer(Of T), event_comb), PT1) = Nothing
        assert(parameter_allocator(i, o))
        Return o
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3) _
                                       (ByVal i As _do_val_val_val_ref(Of PT1, 
                                                                          PT2, 
                                                                          PT3, 
                                                                          T,
                                                                          Boolean),
                                        ByRef o As allocator(Of Func(Of PT2, 
                                                                        PT3, 
                                                                        pointer(Of T), 
                                                                        event_comb), 
                                                                PT1)) As Boolean
        If i Is Nothing Then
            Return False
        Else
            o = make_allocator(Function(v As PT1) As Func(Of PT2, PT3, pointer(Of T), event_comb)
                                   Return Function(p As PT2, p2 As PT3, r As pointer(Of T)) As event_comb
                                              Return sync_async(Function() As Boolean
                                                                    Dim x As T = Nothing
                                                                    Return i(v, p, p2, x) AndAlso
                                                                           eva(r, x)
                                                                End Function)
                                          End Function
                               End Function)
            Return True
        End If
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3) _
                                       (ByVal i As _do_val_val_val_ref(Of PT1, 
                                                                          PT2, 
                                                                          PT3, 
                                                                          T,
                                                                          Boolean)) _
                                       As allocator(Of Func(Of PT2, 
                                                               PT3, 
                                                               pointer(Of T), 
                                                               event_comb), 
                                                       PT1)
        Dim o As allocator(Of Func(Of PT2, PT3, pointer(Of T), event_comb), PT1) = Nothing
        assert(parameter_allocator(i, o))
        Return o
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3) _
                                       (ByVal i As Func(Of PT1, 
                                                           PT2, 
                                                           PT3, 
                                                           T),
                                        ByRef o As allocator(Of Func(Of PT2, 
                                                                        PT3, 
                                                                        pointer(Of T), 
                                                                        event_comb), 
                                                                PT1)) As Boolean
        If i Is Nothing Then
            Return False
        Else
            o = make_allocator(Function(v As PT1) As Func(Of PT2, PT3, pointer(Of T), event_comb)
                                   Return Function(p As PT2, p2 As PT3, r As pointer(Of T)) As event_comb
                                              Return sync_async(Function() As Boolean
                                                                    Return eva(r, i(v, p, p2))
                                                                End Function)
                                          End Function
                               End Function)
            Return True
        End If
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3) _
                                       (ByVal i As Func(Of PT1, 
                                                           PT2, 
                                                           PT3, 
                                                           T)) _
                                       As allocator(Of Func(Of PT2, 
                                                               PT3, 
                                                               pointer(Of T), 
                                                               event_comb), 
                                                       PT1)
        Dim o As allocator(Of Func(Of PT2, PT3, pointer(Of T), event_comb), 
                              PT1) = Nothing
        assert(parameter_allocator(i, o))
        Return o
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3, PT4) _
                                       (ByVal i As _func(Of PT1, 
                                                            PT2, 
                                                            PT3, 
                                                            PT4, 
                                                            pointer(Of T), 
                                                            event_comb),
                                        ByRef o As allocator(Of Func(Of PT2, 
                                                                        PT3, 
                                                                        PT4, 
                                                                        pointer(Of T), 
                                                                        event_comb), 
                                                                PT1)) As Boolean
        If i Is Nothing Then
            Return False
        Else
            o = make_allocator(Function(v As PT1) As Func(Of PT2, PT3, PT4, pointer(Of T), event_comb)
                                   Return Function(p As PT2, p2 As PT3, p3 As PT4, r As pointer(Of T)) As event_comb
                                              Return i(v, p, p2, p3, r)
                                          End Function
                               End Function)
            Return True
        End If
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3, PT4) _
                                       (ByVal i As _func(Of PT1, 
                                                            PT2, 
                                                            PT3, 
                                                            PT4, 
                                                            pointer(Of T), 
                                                            event_comb)) _
                                       As allocator(Of Func(Of PT2, 
                                                               PT3, 
                                                               PT4, 
                                                               pointer(Of T), 
                                                               event_comb), 
                                                       PT1)
        Dim o As allocator(Of Func(Of PT2, PT3, PT4, pointer(Of T), event_comb), PT1) = Nothing
        assert(parameter_allocator(i, o))
        Return o
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3, PT4) _
                                       (ByVal i As _do_val_val_val_val_ref(Of PT1, 
                                                                              PT2, 
                                                                              PT3, 
                                                                              PT4, 
                                                                              T,
                                                                              Boolean),
                                        ByRef o As allocator(Of Func(Of PT2, 
                                                                        PT3, 
                                                                        PT4, 
                                                                        pointer(Of T), 
                                                                        event_comb), 
                                                                PT1)) As Boolean
        If i Is Nothing Then
            Return False
        Else
            o = make_allocator(Function(v As PT1) As Func(Of PT2, PT3, PT4, pointer(Of T), event_comb)
                                   Return Function(p As PT2, p2 As PT3, p3 As PT4, r As pointer(Of T)) As event_comb
                                              Return sync_async(Function() As Boolean
                                                                    Dim x As T = Nothing
                                                                    Return i(v, p, p2, p3, x) AndAlso
                                                                           eva(r, x)
                                                                End Function)
                                          End Function
                               End Function)
            Return True
        End If
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3, PT4) _
                                       (ByVal i As _do_val_val_val_val_ref(Of PT1, 
                                                                              PT2, 
                                                                              PT3, 
                                                                              PT4, 
                                                                              T,
                                                                              Boolean)) _
                                       As allocator(Of Func(Of PT2, 
                                                               PT3, 
                                                               PT4, 
                                                               pointer(Of T), 
                                                               event_comb), 
                                                       PT1)
        Dim o As allocator(Of Func(Of PT2, PT3, PT4, pointer(Of T), event_comb), PT1) = Nothing
        assert(parameter_allocator(i, o))
        Return o
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3, PT4) _
                                       (ByVal i As Func(Of PT1, 
                                                           PT2, 
                                                           PT3, 
                                                           PT4, 
                                                           T),
                                        ByRef o As allocator(Of Func(Of PT2, 
                                                                        PT3, 
                                                                        PT4, 
                                                                        pointer(Of T), 
                                                                        event_comb), 
                                                                PT1)) As Boolean
        If i Is Nothing Then
            Return False
        Else
            o = make_allocator(Function(v As PT1) As Func(Of PT2, PT3, PT4, pointer(Of T), event_comb)
                                   Return Function(p As PT2, p2 As PT3, p3 As PT4, r As pointer(Of T)) As event_comb
                                              Return sync_async(Function() As Boolean
                                                                    Return eva(r, i(v, p, p2, p3))
                                                                End Function)
                                          End Function
                               End Function)
            Return True
        End If
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3, PT4) _
                                       (ByVal i As Func(Of PT1, 
                                                           PT2, 
                                                           PT3, 
                                                           PT4, 
                                                           T)) _
                                       As allocator(Of Func(Of PT2, 
                                                               PT3, 
                                                               PT4, 
                                                               pointer(Of T), 
                                                               event_comb), 
                                                       PT1)
        Dim o As allocator(Of Func(Of PT2, PT3, PT4, pointer(Of T), event_comb), 
                              PT1) = Nothing
        assert(parameter_allocator(i, o))
        Return o
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3, PT4, PT5) _
                                       (ByVal i As _func(Of PT1, 
                                                            PT2, 
                                                            PT3, 
                                                            PT4, 
                                                            PT5, 
                                                            pointer(Of T), 
                                                            event_comb),
                                        ByRef o As allocator(Of _func(Of PT2, 
                                                                         PT3, 
                                                                         PT4, 
                                                                         PT5, 
                                                                         pointer(Of T), 
                                                                         event_comb), 
                                                                PT1)) As Boolean
        If i Is Nothing Then
            Return False
        Else
            o = make_allocator(Function(v As PT1) As _func(Of PT2, PT3, PT4, PT5, pointer(Of T), event_comb)
                                   Return Function(p As PT2,
                                                   p2 As PT3,
                                                   p3 As PT4,
                                                   p4 As PT5,
                                                   r As pointer(Of T)) As event_comb
                                              Return i(v, p, p2, p3, p4, r)
                                          End Function
                               End Function)
            Return True
        End If
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3, PT4, PT5) _
                                       (ByVal i As _func(Of PT1, 
                                                            PT2, 
                                                            PT3, 
                                                            PT4, 
                                                            PT5, 
                                                            pointer(Of T), 
                                                            event_comb)) _
                                       As allocator(Of _func(Of PT2, 
                                                                PT3, 
                                                                PT4, 
                                                                PT5, 
                                                                pointer(Of T), 
                                                                event_comb), 
                                                       PT1)
        Dim o As allocator(Of _func(Of PT2, PT3, PT4, PT5, pointer(Of T), event_comb), PT1) = Nothing
        assert(parameter_allocator(i, o))
        Return o
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3, PT4, PT5) _
                                       (ByVal i As _do_val_val_val_val_val_ref(Of PT1, 
                                                                                  PT2, 
                                                                                  PT3, 
                                                                                  PT4, 
                                                                                  PT5, 
                                                                                  T,
                                                                                  Boolean),
                                        ByRef o As allocator(Of _func(Of PT2, 
                                                                         PT3, 
                                                                         PT4, 
                                                                         PT5, 
                                                                         pointer(Of T), 
                                                                         event_comb), 
                                                                PT1)) As Boolean
        If i Is Nothing Then
            Return False
        Else
            o = make_allocator(Function(v As PT1) As _func(Of PT2, PT3, PT4, PT5, pointer(Of T), event_comb)
                                   Return Function(p As PT2,
                                                   p2 As PT3,
                                                   p3 As PT4,
                                                   p4 As PT5,
                                                   r As pointer(Of T)) As event_comb
                                              Return sync_async(Function() As Boolean
                                                                    Dim x As T = Nothing
                                                                    Return i(v, p, p2, p3, p4, x) AndAlso
                                                                           eva(r, x)
                                                                End Function)
                                          End Function
                               End Function)
            Return True
        End If
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3, PT4, PT5) _
                                       (ByVal i As _do_val_val_val_val_val_ref(Of PT1, 
                                                                                  PT2, 
                                                                                  PT3, 
                                                                                  PT4, 
                                                                                  PT5, 
                                                                                  T,
                                                                                  Boolean)) _
                                       As allocator(Of _func(Of PT2, 
                                                                PT3, 
                                                                PT4, 
                                                                PT5, 
                                                                pointer(Of T), 
                                                                event_comb), 
                                                       PT1)
        Dim o As allocator(Of _func(Of PT2, PT3, PT4, PT5, pointer(Of T), event_comb), PT1) = Nothing
        assert(parameter_allocator(i, o))
        Return o
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3, PT4, PT5) _
                                       (ByVal i As _func(Of PT1, 
                                                            PT2, 
                                                            PT3, 
                                                            PT4, 
                                                            PT5, 
                                                            T),
                                        ByRef o As allocator(Of _func(Of PT2, 
                                                                         PT3, 
                                                                         PT4, 
                                                                         PT5, 
                                                                         pointer(Of T), 
                                                                         event_comb), 
                                                                PT1)) As Boolean
        If i Is Nothing Then
            Return False
        Else
            o = make_allocator(Function(v As PT1) As _func(Of PT2, PT3, PT4, PT5, pointer(Of T), event_comb)
                                   Return Function(p As PT2,
                                                   p2 As PT3,
                                                   p3 As PT4,
                                                   p4 As PT5,
                                                   r As pointer(Of T)) As event_comb
                                              Return sync_async(Function() As Boolean
                                                                    Return eva(r, i(v, p, p2, p3, p4))
                                                                End Function)
                                          End Function
                               End Function)
            Return True
        End If
    End Function

    Public Function parameter_allocator(Of T, PT1, PT2, PT3, PT4, PT5) _
                                       (ByVal i As _func(Of PT1, 
                                                            PT2, 
                                                            PT3, 
                                                            PT4, 
                                                            PT5, 
                                                            T)) _
                                       As allocator(Of _func(Of PT2, 
                                                                PT3, 
                                                                PT4, 
                                                                PT5, 
                                                                pointer(Of T), 
                                                                event_comb), 
                                                       PT1)
        Dim o As allocator(Of _func(Of PT2, PT3, PT4, PT5, pointer(Of T), event_comb), 
                              PT1) = Nothing
        assert(parameter_allocator(i, o))
        Return o
    End Function
End Module
